
package rentalgui;

import database.LeaseDao;
import database.PaymentDao;
import database.TenantDao;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import rentaldb.Lease;
import rentaldb.Payment;
import rentaldb.Tenant;

public class PaymentInputWindow extends javax.swing.JFrame {

    /**
     * Creates new form ExpenseInputWindow
     */
    public PaymentInputWindow(LeaseDao ldao, PaymentDao paydao, TenantDao tdao) {
        initComponents();
        this.ldao = ldao;
        this.paydao = paydao;
        this.tdao = tdao;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        inputDueDate = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        buttonClear = new javax.swing.JButton();
        buttonEnter = new javax.swing.JButton();
        buttonClose = new javax.swing.JButton();
        inputDatePaid = new javax.swing.JTextField();
        inputType = new javax.swing.JTextField();
        inputAmount = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        inputTenantId = new javax.swing.JTextField();
        inputLeaseId = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Payment Input");
        setResizable(false);

        jLabel2.setText("Due Date");

        jLabel3.setText("Date Paid");

        jLabel4.setText("Type");

        jLabel5.setText("Amount");

        buttonClear.setText("Clear");
        buttonClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClearActionPerformed(evt);
            }
        });

        buttonEnter.setText("Enter Data");
        buttonEnter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEnterActionPerformed(evt);
            }
        });

        buttonClose.setText("Close Window");
        buttonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCloseActionPerformed(evt);
            }
        });

        jLabel7.setText("Tenant ID");

        jLabel8.setText("Lease ID");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(buttonClear, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonEnter, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonClose))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputLeaseId, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inputTenantId, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inputDueDate, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inputDatePaid)
                            .addComponent(inputType)
                            .addComponent(inputAmount))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputDueDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(inputDatePaid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(inputType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(inputAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputTenantId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(inputLeaseId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonEnter)
                    .addComponent(buttonClear)
                    .addComponent(buttonClose))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearActionPerformed
        inputDueDate.setText("");
        inputDatePaid.setText("");
        inputType.setText("");
        inputAmount.setText("");
        inputTenantId.setText("");
        inputLeaseId.setText("");
    }//GEN-LAST:event_buttonClearActionPerformed

    private void buttonEnterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEnterActionPerformed
        if(allFieldsAreFull()){
            try{
                DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
                Date dueDate = df.parse(inputDueDate.getText().trim());
                Date datePaid = df.parse(inputDatePaid.getText().trim());
                String type = inputType.getText().trim();
                Double amount = Double.parseDouble(inputAmount.getText().trim());
                Integer tenantId = Integer.parseInt(inputTenantId.getText().trim());
                Tenant tenant = tdao.getTenantById(tenantId);
                Integer leaseId = Integer.parseInt(inputLeaseId.getText().trim());
                Lease lease = ldao.getLeaseById(leaseId);
                
                Payment payment = new Payment();
                payment.setDuedate(dueDate);
                payment.setDatepaid(datePaid);
                payment.setType(type);
                payment.setAmount(amount);
                payment.setTenantid(tenant);
                payment.setLeaseid(lease);
                paydao.persist(payment);
                JOptionPane.showMessageDialog(null, "Input successfully added!", "Input Successful", JOptionPane.INFORMATION_MESSAGE);
            }
            catch (Exception e){
                JOptionPane.showMessageDialog(null, "Not all input processed correctly.", "Input Error", JOptionPane.ERROR_MESSAGE);
            }
        } else{
            JOptionPane.showMessageDialog(this, "All fields must be full before submitting.", "ALERT", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_buttonEnterActionPerformed

    private void buttonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCloseActionPerformed
        dispose();
    }//GEN-LAST:event_buttonCloseActionPerformed

    private boolean allFieldsAreFull(){
        return !inputDueDate.getText().trim().isEmpty()
                && !inputDatePaid.getText().trim().isEmpty()
                && !inputType.getText().trim().isEmpty()
                && !inputAmount.getText().trim().isEmpty()
                && !inputTenantId.getText().trim().isEmpty()
                && !inputLeaseId.getText().trim().isEmpty();                
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonClear;
    private javax.swing.JButton buttonClose;
    private javax.swing.JButton buttonEnter;
    private javax.swing.JTextField inputAmount;
    private javax.swing.JTextField inputDatePaid;
    private javax.swing.JTextField inputDueDate;
    private javax.swing.JTextField inputLeaseId;
    private javax.swing.JTextField inputTenantId;
    private javax.swing.JTextField inputType;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    // End of variables declaration//GEN-END:variables
    private LeaseDao ldao;
    private PaymentDao paydao;
    private TenantDao tdao;
}
